<template>
	<view class="setting-container" :style="{height: `${windowHeight}px`}">
		<view class="menu-list">
			<!--      <view class="list-cell list-cell-arrow" @click="handleToPwd">
        <view class="menu-item-box">
          <view class="iconfont icon-password menu-icon"></view>
          <view>修改密码</view>
        </view>
      </view>
    -->
			<view class="list-cell list-cell-arrow" @click="clearStorage">
				<view class="menu-item-box">
					<view class="iconfont icon-clean menu-icon"></view>
					<view>清理缓存:{{fileSizeString}}</view>
				</view>
			</view>
			<view class="list-cell list-cell-arrow" @click="upgrade">
				<view class="menu-item-box">
					<view class="iconfont icon-refresh menu-icon"></view>
					<view>检查更新</view>
				</view>
			</view>
		</view>
		<view class="cu-list menu">
			<view class="cu-item item-box">
				<view class="content text-center" @click="handleLogout">
					<text class="text-black">退出登录</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import checkVersion from '@/pages/lq-upgrade/checkVersion.js'
	import {
		cheakFile
	} from '@/api/system/cheakload.js'
	import config from '@/config'
	export default {
		data() {
			return {
				windowHeight: uni.getSystemInfoSync().windowHeight,
				fileSizeString:""
			}
		},
		created() {
			this.formatSize();
		},
		methods: {
			// 获取缓存
			formatSize() {
				plus.cache.calculate((size) => {
					let sizeCache = parseInt(size);
					if (sizeCache == 0) {
						this.fileSizeString = "0B";
					} else if (sizeCache < 1024) {
						this.fileSizeString = sizeCache + "B";
					} else if (sizeCache < 1048576) {
						this.fileSizeString = (sizeCache / 1024).toFixed(2) + "KB";
					} else if (sizeCache < 1073741824) {
						this.fileSizeString = (sizeCache / 1048576).toFixed(2) + "MB";
					} else {
						this.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "GB";
					}
				});
			},
			async upgrade() {
				var a = {}
				cheakFile().then(res => {
					// 获取系统信息
					var version = uni.getStorageSync('version');
					var versionCode = uni.getStorageSync('versionCode');
					console.log(res)
					if (res.length > 0) {
						if (versionCode < res[0].versionnumber) {
							let name = res[0].versionname; //新版本名称
							let code = res[0].versionnumber; //新版本号（数字）
							let content = res[0].content; //更新内容
							let url = config.baseUrl_load + "/file/download?fileName=" + res[0]
								.fileurl; //app下载链接，（演示请换成真正在下载链接）
							//let url ="http://60.205.104.240:8080/common/download?fileName=UNI20220706200750.apk&delete=false"
							console.log(url)
							let forceUpdate = res[0].forceupdate; //是否强制更新
							a = {
								name, //最新版本名称
								code, //最新版本号
								content, //更新内容
								url, //下载链接
								forceUpdate //是否强制升级			  
							}
							checkVersion(a)
						} else {
							uni.showToast({
								title: '暂无更新',
								icon: 'none',
								duration: 2000,
							})
						}
					} else {
						uni.showToast({
							title: '暂无更新',
							icon: 'none',
							duration: 2000,
						})
					}

				}).catch(error => {
					reject(error)
				})

			},
			handleToPwd() {
				this.$tab.navigateTo('/pages/mine/pwd/index')
			},
			handleToUpgrade() {
				this.$modal.showToast('模块建设中~')
			},
			clearStorage() {
				uni.showModal({
					title: '清除缓存',
					content: '您确定要清除缓存吗？',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							this.clearCache();
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			clearCache() {
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i];
						plus.io.resolveLocalFileSystemURL(filePath, (entry) => {
							if (entry.isDirectory) {
								entry.removeRecursively((entry) => {
									uni.showToast({
										title: '缓存清理完成',
										duration: 2000
									});
									this.formatSize();
								}, (e) => {
									console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, (e) => {
							console.log('文件路径读取失败')
						});
					}
				} else { // ios  
					plus.cache.clear(() => {
						uni.showToast({
							title: '缓存清理完成',
							duration: 2000
						});
						this.formatSize();
					});
				}
			},
			handleLogout() {
				this.$modal.confirm('确定注销并退出系统吗？').then(() => {
					this.$store.dispatch('LogOut').then(() => {
						this.$tab.reLaunch('/pages/index')
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		background-color: #f8f8f8;
	}

	.item-box {
		background-color: #FFFFFF;
		margin: 30rpx;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding: 10rpx;
		border-radius: 8rpx;
		color: #303133;
		font-size: 32rpx;
	}
</style>